package com.gency.subscribe.web.controller.system;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gency.subscribe.core.util.system.OSSClientUtils;
import com.gency.subscribe.model.system.InvestmentbankBase;
import com.gency.subscribe.model.system.PageBean;
import com.gency.subscribe.service.system.IInvestmentbankBaseService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 马行空
 * @since 2018-06-18
 */
@Controller
@RequestMapping("/investmentbank_base")
public class InvestmentbankBaseController {
	
	
	@Autowired
	IInvestmentbankBaseService investmentbankBaseService;
	
	
	@GetMapping("/list")
	public ModelAndView list(PageBean pb,ModelAndView mv){
		Page<InvestmentbankBase> page = new Page<InvestmentbankBase>(pb.getCurPage(),pb.getPageSize());
		page.setAsc(false);
		page.setOrderByField("create_time");
		EntityWrapper<InvestmentbankBase> entityWrapper = new EntityWrapper<>();
		entityWrapper.eq("type", 1);
		page = investmentbankBaseService.selectPage(page,entityWrapper);
		List<InvestmentbankBase> list = page.getRecords();
		pb = new PageBean(page.getTotal(), page.getPages(), page.getCurrent(), 10, page.getOffsetCurrent(), page.getOffset(),0);
		mv.addObject("pagebean",pb);
		mv.addObject("investmentlist", list);
		mv.setViewName("invesbank/list");
		return mv;
	}
	
	@GetMapping("/add")
	public ModelAndView add(ModelAndView mv){
		mv.setViewName("invesbank/info");
		mv.addObject("investmentbankBase", new InvestmentbankBase());
		return mv;
	}
	
	
	@PostMapping("/add")
	public ModelAndView add(ModelAndView mv,InvestmentbankBase serverBase,MultipartFile multipartFile){
		mv.setViewName("redirect:list");
		String url ="";
		try {
			if(multipartFile != null && !multipartFile.isEmpty()){
				url = OSSClientUtils.URL_PREFIX+OSSClientUtils.putOSSObject(multipartFile);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		serverBase.setImage_url(url);
		serverBase.setType(1);
		investmentbankBaseService.insert(serverBase);
		return mv;
	}
	
	
	@GetMapping("/update")
	public ModelAndView update(ModelAndView mv,Long id){
		mv.setViewName("invesbank/info");
		mv.addObject("investmentbankBase",investmentbankBaseService.selectById(id));
		return mv;
	}
	
	
	@PostMapping("/update")
	public ModelAndView update(ModelAndView mv,InvestmentbankBase serverBase,MultipartFile multipartFile){
		mv.setViewName("redirect:list");
		String url ="";
		try {
			if(multipartFile != null && !multipartFile.isEmpty()){
				url = OSSClientUtils.URL_PREFIX+OSSClientUtils.putOSSObject(multipartFile);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		if(!url.equals("")){
			serverBase.setImage_url(url);
		}
		serverBase.setType(1);
		investmentbankBaseService.updateById(serverBase);
		return mv;
	}
	
	@GetMapping("/delete")
	public ModelAndView delete(ModelAndView mv,Long id){
		mv.setViewName("redirect:list");
		investmentbankBaseService.deleteById(id);
		return mv;
	}
	
	
	@GetMapping("/sdopOrOpen")
	public ModelAndView sdopOrOpen(ModelAndView mv,Long id,Integer type){
		mv.setViewName("redirect:list");
		if(id == null || type == null){
			return mv;
		}
		InvestmentbankBase policyBase = new InvestmentbankBase();
		policyBase.setId(id);
		policyBase.setFlag_open(type);
		investmentbankBaseService.updateById(policyBase);
		return mv;
	}

}

